#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <string>
#include <iostream>

using namespace std;

#include "topdbs.hpp"

void format_placeholders_test()
{
	string sSQL = "select * from topdbs_test where prodcd = :1 and prodname = $2 and cc = ? ";
	{
		printf("===topdbs_oracle===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders_old(topdbs_oracle,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}
	{
		printf("===topdbs_sqlite===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders_old(topdbs_sqlite,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}
	{
		printf("===topdbs_informix===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders_old(topdbs_informix,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	

	{
		printf("===topdbs_mysql===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders_old(topdbs_mysql,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	

	{
		printf("===topdbs_postgresql===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders_old(topdbs_postgresql,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}		
}

void format_placeholders_vv_test()
{
	printf("============format_placeholders_vv_test============\n");
	string sSQL = "select * from topdbs_test where aa='bbb?' and prodcd = :1 and prodname = $2 and cc = ? and dd = 'abc?';";

	printf("sSQL=%s\n",sSQL.c_str());
	int params_num = cvtGetPreParamCount(sSQL);	
	printf("params_num = %d\n",params_num);

	{
		printf("===topdbs_oracle===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_oracle,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}
	{
		printf("===topdbs_sqlite===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_sqlite,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}
	{
		printf("===topdbs_informix===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_informix,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	

	{
		printf("===topdbs_mysql===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_mysql,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	

	{
		printf("===topdbs_postgresql===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_postgresql,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	

	{
		sSQL = "insert int topdbs_test values ('?abnc' ,$2 , ?,'abc?') ";
		printf("===topdbs_oracle===\n");
		string sCommandText = sSQL;
		printf("sCommandText=%s\n",sCommandText.c_str());
		format_placeholders(topdbs_oracle,sCommandText);
		printf("sCommandText=%s\n\n",sCommandText.c_str());
	}	
}

void cvtGetPreParamCount_test()
{
	string sSQL = "select * from topdbs_test where aa='bbb?' and prodcd = :1 and prodname = $2 and cc = ? and dd = 'abc?';";
	printf("sSQL=%s\n",sSQL.c_str());
	int params_num = cvtGetPreParamCount(sSQL);	
	printf("params_num = %d\n",params_num);

	vector<string> v;
	cvtPrepareParams(sSQL,v);
	cvtBindParams(sSQL,v,0,"v1");
	cvtBindParams(sSQL,v,1,"v2");	
	cvtBindParams(sSQL,v,2,"v3");	

	cvtFormatParams(sSQL,v);
	printf("new_sSQL=%s\n",sSQL.c_str());

	sSQL = "insert int topdbs_test values ('?abnc' ,$2 , ?,'abc?','ccabc?') ";
	printf("sSQL=%s\n",sSQL.c_str());
	params_num = cvtGetPreParamCount(sSQL);	
	printf("params_num = %d\n",params_num);
}

int main()
{
	//format_placeholders_test();
	//format_placeholders_vv_test();
	cvtGetPreParamCount_test();
	return 0;
}